﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AmaroK86.ImageFormat;
using AmaroK86.MassEffect3;
using AmaroK86.MassEffect3.PCC;
using AmaroK86.MassEffect3.ZlibBlock;
using AmaroK86.MassEffect3.UnrealObjects;
using Gibbed.IO;

namespace AmaroK86.MassEffect3.Texture
{
    public class TextureHandler
    {
        string archivePath;
        List<string> pccPathList;
        Texture2D texture;

        public TextureHandler(string newTexFileName, string textureName, List<string> pccPathList, string archivePath)
        {
            ImageMipMapHandler imgMipMap = new ImageMipMapHandler(newTexFileName);

            List<Texture2D.ImageInfo> imgInfoList = new List<Texture2D.ImageInfo>();

            using (FileStream archive = File.OpenWrite(archivePath))
            {
                archive.Seek(0, SeekOrigin.End);

                // starts from the smaller image
                for (int i = imgMipMap.imageList.Count - 1; i >= 0; i--)
                {
                    ImageFile newImageFile = imgMipMap.imageList[i];

                    // insert images only with size > 64
                    if (newImageFile.imgSize.width < 64 && newImageFile.imgSize.height < 64)
                        continue;

                    //WARNING, WORKS ONLY WITH DDS FILES!!!!
                    byte[] buffer = ZBlock.Compress(newImageFile.imgData);

                    Texture2D.ImageInfo imageInfo = new Texture2D.ImageInfo();
                    imageInfo.storageType = Texture2D.storage.arcCpr;
                    imageInfo.imgSize = newImageFile.imgSize;
                    imageInfo.uncSize = newImageFile.resize().Length;
                    imageInfo.cprSize = buffer.Length;
                    imageInfo.offset = (int)archive.Position;
                    imageInfo.pccImage = null;

                    archive.WriteBytes(buffer);
                    imgInfoList.Add(imageInfo);
                }
            }

            foreach (string pccFilePath in pccPathList)
            {
                PCCObject pccObj = new PCCObject(pccFilePath);

                /*Console.WriteLine("Name: " + pccObj.Exports[texIndex].ObjectName +
                                  "\n  Class: " + pccObj.Exports[texIndex].ClassName +
                                  "\n  Parent Class: " + pccObj.Exports[texIndex].ClassParent +
                                  "\n  Package: " + pccObj.Exports[texIndex].PackageName +
                                  "\n  Full Package: " + pccObj.Exports[texIndex].PackageFullName);*/

                Texture2D tex2D = new Texture2D(pccObj.Exports[textureName]);
                Texture2D.ImageList newImageList = new Texture2D.ImageList();
                newImageList.AddRange(tex2D.imgList.Where(image => image.imgSize.width < 64 && image.imgSize.height < 64).Reverse().ToList());
                newImageList.AddRange(imgInfoList);
                newImageList.Reverse();
                tex2D.imgList = newImageList;

                tex2D.properties["LODGroup"].Value = "TEXTUREGROUP_Shadowmap";
                (tex2D.properties["LODGroup"] as ByteType).val2 = 0;
                
                Console.WriteLine("LODGroup: " + tex2D.properties["LODGroup"].Value + " - " + (tex2D.properties["LODGroup"] as ByteType).val2);

                tex2D.commitChanges();

                pccObj.saveToFile(true);
            }

            Console.ReadLine();
        }
    }
}
